package cn.edu.gzist.chapter08.service;

import cn.edu.gzist.chapter08.entity.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class MessageConsumerService {
    /**
     * Publish/Subscribe模式接收，处理邮件业务
     */
/*    @RabbitListener(queues = "email.queue")
    public void psubConsumerEmail(Message message) {
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println("邮件业务接收到消息： "+s);

    }*/
    /**
     * Publish/Subscribe模式接收，处理短信业务
     */
/*    @RabbitListener(queues = "sms.queue")
    public void psubConsumerSms(Message message) {
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println("短信业务接收到消息： "+s);
    }*/

    @RabbitListener(bindings =@QueueBinding(value =@Queue("email.queue"),
            exchange =@Exchange(value = "pub/sub.exchange",type = "fanout")))
    public void psubConsumerEmail(User user) {
        System.out.println("邮件业务接收到消息： "+user);

    }

    @RabbitListener(bindings =@QueueBinding(value =@Queue("sms.queue"),
            exchange =@Exchange(value = "pub/sub.exchange",type = "fanout")))
    public void psubConsumerSms(User user) {
        System.out.println("短信业务接收到消息： "+user);
    }

    /**
     *  路由模式消息接收，处理error级别日志信息
     */
    @RabbitListener(bindings =@QueueBinding(value =@Queue("routing.error.queue"),
            exchange =@Exchange(value = "routing.exchange",type = "direct"),
            key = "routingkey.error"))
    public void routingConsumerError(String message) {
        System.out.println("接收到error级别日志消息： "+message);
    }
    /**
     *  路由模式消息接收，处理info、error、warning级别日志信息
     */
    @RabbitListener(bindings =@QueueBinding(value =@Queue("routing.all.queue"),
            exchange =@Exchange(value = "routing.exchange",type = "direct"),
            key = {"routingkey.error","routingkey.info","routingkey.warning"}))
    public void routingConsumerAll(String message) {
        System.out.println("接收到info、error、warning等级别日志消息： "+message);
    }
    /**
     *  通配符模式消息接收，进行邮件业务订阅处理
     */
    @RabbitListener(bindings =@QueueBinding(value =@Queue("topic.email.queue"),
            exchange =@Exchange(value = "topic.exchange",type = "topic"),
            key = "info.#.email.#"))
    public void topicConsumerEmail(String message) {
        System.out.println("接收到邮件订阅需求处理消息： "+message);
    }
    /**
     *  通配符模式消息接收，进行短信业务订阅处理
     */
    @RabbitListener(bindings =@QueueBinding(value =@Queue("topic.sms.queue"),
            exchange =@Exchange(value = "topic.exchange",type = "topic"),
            key = "info.#.sms.#"))
    public void topicConsumerSms(String message) {
        System.out.println("接收到短信订阅需求处理消息： "+message);
    }


}